function qttArticleSubmit(data = ''){
	let postUrl = 'https://mpapi.qutoutiao.net/content/saveNew';
	result = '';
	$.ajax({
		url:postUrl,
		type:"post",
		async:false,
	    data:data,
		success:function(data){
			if(data.code == 0){
				result = true;
			}else{
				result = data.message;
			}
		},
		error:function(){
			layer.msg('头条文章抛送异常');
		}
	})
	return result;
}

function qttArticleArrange(data = ''){
	
	let title = data.title;
	let tags = data.tags;
	let content = '';
	let images = [];
	token = getQttToken();
	for(item of data.items){
		if(item.type == 1){
			content = content+
			`<p>${item.content}</p`;
		}
		if(item.type == 2){
			let imageObj = qttImageUpload(item.content);
			if(!imageObj){
				layer.msg('上传出错');
				return false;
			}
			content = content+
			`<p><br/><img data-src="${imageObj.url}" data-size="${imageObj.height},${imageObj.width}" alt="" title="" id=""/></p>`;
			images.push(imageObj.path);
		}
	}
	
	//算出cover字符串
	cover_str = '',
	image_list_str = '',
	image_len = images.length;
	if(images.length<3){
		cover_str = JSON.stringify([images[0]]);
	}else{
		cover_str = JSON.stringify([images[0],images[1],images[2]]);
	}
	cover_str = JSON.stringify([images[0]]);
	image_list_str = JSON.stringify(images);
		
	data = {
		category: 24,
		two_level_category: JSON.stringify([{"id":"102400","name":"服饰"},{"id":"102414","name":"其他"}]),
		cover_type: 1,
		cover: cover_str,
		title: title,
		detail: content,
		image_list: image_list_str,
		is_origin: 1,
        tag: tags,
		user_enjoy:1,
		haveRepeatCoverImg: false,
		isPublishArticle: true,
		token:token,
		dtu: 200,
		is_publish:1
	};
	return data;
}

function qttArticle(){
	let token = getCurrentToken();
	let postUrl = 'https://cutezz.cn/admin/article/item?token='+token+'&platform=趣头条';
	result = '';
	$.ajax({
		url:postUrl,
		type:"get",
		async:false,
		success:function(data){
			if(data.code == 200){
				if(data.result){
					result = data.result;
				}
				
			}
			console.log(data);
		},
		error:function(){
			layer.msg('头条文章获取异常');
		}
	})
	return result;
}

function qttImageUpload(imageUrl = ''){
	token = getQttToken();
	if(!token){
		layer.msg('暂无token,流程取消');
		return false;
	}
	let url = 'https://qupload.qutoutiao.net/api/v2.0/image/grab';
	result = '';
	if(imageUrl.indexOf('http') == -1){
		imageUrl = 'https:'+imageUrl;
	}
	data = {
		image:'["'+imageUrl+'"]',
		dtu:200,
		token:token,
	};
	$.ajax({
		url:url,
		type:"post",
		async:false,
	    data:data,
		success:function(data){
			if(data.code == 0){
				for(let i in data.data){
					result = data.data[i];
				}
			}
		},
		error:function(){
			layer.msg('图片上传异常');
		}
	})
	return result;
}

//状态回写
function qttStatusWrite(article_id = ''){
	token = getCurrentToken();
	let url = 'https://cutezz.cn/admin/article/status';
	result = '';
	data = {
		platform:'趣头条',
		article_id:article_id,
		token:token,
	};
	$.ajax({
		url:url,
		type:"post",
		async:false,
	    data:data,
		success:function(data){
			if(data.code == 200){
				result = true;
			}
			
		},
		error:function(){
			layer.msg('头条状态回写异常');
		}
	})
	return result;
}

function getQttToken(){
	token = '';
	current_cookie = document.cookie;
	if(current_cookie.indexOf(';') != -1){
		cookie_arr = current_cookie.split(';');
		for(cookie_item of cookie_arr){
			if(cookie_item.indexOf('token=') != -1){
				token = cookie_item.split('token=')[1];
				break;
			}
		}
	}
	return token;
}
